import fs from 'fs'
import serverConfig from '../../config/server.config';
import base from '../../export/base.json'

/**
 * @class ReadAssets2ConfigService
 * @desc 读取文件生成配置
 */
class ReadAssets2ConfigService {
  /** @type {Config.Instance} */
  json = {
    config: {
      baseUrl: 'http://localhost:3306/',
      defaultPendantMax: base.defaultPendantMax,
      designHeight: 2384,
      designWidth: 2685
    },
    groups: [],
    items: [],
  };

  /**
   * @param assetsURL 资源路径
   * @param baseConfig 配置
   */
  constructor(assetsURL: string, baseConfig: Partial<Config.ReadAssetOption> = {}) {
    console.log(base);

    this.json.config = {...this.json.config, ...baseConfig}

    this.read(assetsURL)
  }

  read(assetsURL: string) {
    try {
      const result: Record<string, Config.Instance> = {}
      // 读取imgs目录，获取默认组别名
      const root = fs.readdirSync(assetsURL)
      // 若有多组数据，则循环生成多组配置
      for (const rootItem of root) {
        if (rootItem !== '.DS_Store') {
          const groups: Config.Group[] = []
          const items: Config.Item[] = []
          const rootFileName = rootItem

          // 部位list
          const positionsPath = fs.readdirSync(`./${assetsURL}/${rootFileName}`)
          // console.log(1, positionsPath);
          // 部位循环
          for (const positionItem of positionsPath) {
            if (positionItem !== '.DS_Store') {
              // 生成一条新的部位数据
              const parentData: Config.Group = {
                id: groups.length,
                name: positionItem,
                zIndex: groups.length,
                rid: undefined,
                advId: undefined,
                isRequired: 0,
                isExportJson: 1,
                childCount: 0,
              }
              // 获取装饰list
              const pendantsPath = fs.readdirSync(`./${assetsURL}/${rootFileName}/${positionItem}`)
              // 装饰循环
              for (const pendantItem of pendantsPath) {
                if (pendantItem !== '.DS_Store') {
                  items.push({
                    id: items.length,
                    name: pendantItem,
                    max: this.json.config.defaultPendantMax,
                    gid: parentData.id,
                    gName: parentData.name,
                    url: `${rootItem}/${parentData.name}/${pendantItem}`,
                    currentCount: 0
                  })
                }
              }

              parentData.childCount = pendantsPath.length
              parentData.isRequired = !!pendantsPath.length ? 1 : 0
              groups.push(parentData)
            }
          }

          result[rootItem as string] = {
            ...this.json,
            groups,
            items,
          }
        }
      }

      const fileName = `${serverConfig.app.exportUrl}/metadata.json`
      fs.openSync(fileName, 'w+')
      fs.writeFileSync(fileName, JSON.stringify(result, {}, 2))
    } catch (error) {
      console.log(error);
    }
  }
}

export default ReadAssets2ConfigService
